package lab.cloud.project.share;

import java.io.*;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;
import sun.net.*;
import sun.net.ftp.*;

public class FtpStore extends SwingWorker<Void, Void> implements FileStoreInterface {

    ClientView cv = null;
	String ftpServerAddr;
	int ftpServerPort;
	String user = "anonymous";
	String password = "ResearchShare@cloud.lab";
	FtpClient ftpClient = null;
    String local;
    String remote;
    boolean upDown;
    JButton okButton = null;
    JButton cancelButton = null;
    JTextArea descriptionArea = null;
    JDialog rootDialog = null;

    String fs = System.getProperties().getProperty("file.separator");
	
	public FtpStore(String addr, int port, boolean ud){
		ftpServerAddr = addr;
		ftpServerPort = port;
		try{
			ftpClient = new FtpClient();
			ftpClient.openServer(ftpServerAddr, ftpServerPort);
			ftpClient.login(user, password);
			ftpClient.binary();
		}catch(IOException e){
			e.printStackTrace();
		}
        upDown = ud;
	}
    public void setOkButton(JButton jb){
        okButton = jb;
    }
    public void setCloseButton(JButton jb){
        cancelButton = jb;
    }
    public void setDescription(JTextArea jta){
        descriptionArea = jta;
    }
    public void setDialog(JDialog jd){
        rootDialog = jd;
    }

    public String getLocal(){
        return local;
    }
    public String getRemote(){
        return remote;
    }
    public void setLocal(String l){
        local = l;
    }
    public void setRemote(String r){
        remote = r;
    }
	@Override
	public void download(String remote, String local) {
		try{
            int downloaded = 0;
			TelnetInputStream is = ftpClient.get(remote);
            int all = is.available();
			File fileOut = new File(local);
			FileOutputStream os = new FileOutputStream(fileOut);
			byte[] buffer = new byte[64];
			int c;
			while ((c = is.read(buffer)) != -1){
				os.write(buffer, 0, c);
                downloaded += c;
                int progress = (int) downloaded * 100/all;
                setProgress(progress);
			}	
			is.close();
			os.close();
            ftpClient.closeServer();
		}catch(IOException e){
			e.printStackTrace();
		}
	}

	@Override
	public void upload(String local, String remote) {
		try{
            okButton.setEnabled(false);
            cancelButton.setEnabled(false);
            descriptionArea.setEditable(false);
            descriptionArea.setEnabled(false);
            
            int uploaded = 0;
			String[] dirs = remote.split(fs);
            remote = dirs[dirs.length - 1];
            String path = "";
            for (int i = 0; i < dirs.length - 1; i++)
                path = path + dirs[i] + fs;

            ftpClient.cd(path);
            TelnetOutputStream os = ftpClient.put(remote);

            File fileInput = new File(local);
			FileInputStream is = new FileInputStream(fileInput);
            int all = is.available();
			byte[] buffers = new byte[64];
			int c;
			while ((c = is.read(buffers)) != -1){
				os.write(buffers, 0, c);
                uploaded += c;
                int progress = (int) uploaded * 100/all;
                setProgress(progress);
			}
			is.close();
			os.close();
            ftpClient.closeServer();
		}catch(IOException e){
			e.printStackTrace();
		}
	}

    @Override
    public Void doInBackground() {
        setProgress(0);
        if (upDown)
            upload(local, remote);
        else
            download(remote, local);
        return null;
    }
    @Override
    public void done(){
        rootDialog.dispose();
    }

}
